package wan.com.templateproject.hander;

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;

import java.util.concurrent.TimeUnit;

/**
 * @author wan
 * @version 1.0.0
 * @Description 发送redis消息
 * @createTime 2021年10月14日 18:10:23
 */
@Slf4j
@NoArgsConstructor
public class SendReddisHander implements Runnable{

    private String key;

    private String value;

    private RedissonClient redissonClient;

    public SendReddisHander(String key, String value, RedissonClient redissonClient) {
        this.key = key;
        this.value = value;
        this.redissonClient = redissonClient;
    }

    @Override
    public void run() {
        boolean boo = false;
        RLock lock = redissonClient.getLock("lock");
        try {
            boo = lock.tryLock(10, 300, TimeUnit.SECONDS);
            if (boo) {
                log.info("加锁成功");
                log.info("加锁成功");
                log.info("即将睡眠");
                Thread.sleep(3000);
                log.info("已睡醒");
            }
        }catch (InterruptedException e) {
            e.getMessage();
        }finally {
            if (boo) {
                lock.unlock();
                log.info("解锁成功");
            }
        }
    }

//    @Override
//    public void run() {
//        log.info("线程{}开始执行,开始向redis队列写入{}", Thread.currentThread().getName(), key);
//        System.out.println(jedis+" "+key+" "+value);
//        String set = jedis.set(key, value);
//        if ("OK".equals(set)) {
//            log.info("向redis写入 {} 成功《《《《《《《《《", key);
//        }else {
//            log.info("向redis写入 {} 失败《《《《《《《《《", key);
//        }
//    }

}
